package tech.novastar.http;

import tech.novastar.domain.Store;
import tech.novastar.service.StoreService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 库存的变更
 */
@WebServlet("/StoreUpdateServlet")
public class StoreUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int count = Integer.valueOf(request.getParameter("count"));
        StoreService storeService = new StoreService();
        storeService.updateById(id, count);
        response.sendRedirect(request.getContextPath() + "/home");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        StoreService storeService = new StoreService();
        Store store = storeService.findById(id);
        request.setAttribute("store", store);
        request.getRequestDispatcher("/pages/update.jsp").forward(request, response);
    }
}

